<?php

/**
 * Model m_content
 *
 * @author hoanguyen
 * @since 1, 2014
 */
class M_content extends CI_Model {

    private $mdpClient;

    function __construct() {
        parent::__construct();
        $this->load->library('pb4php/Pb4php');
        $this->load->library('proto/Proto');
        $this->load->library('zmq/ZmqMDClient', array());

        $mdClient = $this->config->item('mdClient');
        $verbose = $mdClient['verbose'];
        $brokerArray = $mdClient['brokerList'];
        $currentBroker = 0;
        $this->mdpClient = new ZmqMDClient($brokerArray[$currentBroker], $verbose);
        $this->mdpClient->connect_to_broker();
    }

    function get_message_reply($messageReply) {
        $contentMessageReply = New ContentItem();
        $contentMessageReply->ParseFromString($messageReply->last());
        $uuid = $contentMessageReply->uuid();
        log_message("debug", "[$uuid][ContentItemReply]: " . $contentMessageReply->toJson());

        $ItemCRUDReply = New ItemCRUDReply();
        $ItemCRUDReply->ParseFromString($contentMessageReply->message());
        log_message("info", "[$uuid][ItemCRUDReply]: " . $ItemCRUDReply->toJson());

        return $ItemCRUDReply;
    }

    /*     * *************************************************************************
     * parse_reply
     *
     * Parsing reply
     * @param string $reply
     * @return multitype:RageComicAlbumItem multitype:NULL number  unknown |ItemCRUDReply
     * ************************************************************************* */

    function parse_reply($reply) {
        // parse ContentItem
        $contentItemReply = new ContentItem();
        $contentItemReply->ParseFromString($reply);
        $uuid = $contentItemReply->uuid();
        log_message("debug", "[$uuid] Start parsing reply.");
        log_message("debug", "[$uuid][ContentItem] " . $contentItemReply->toJson());
        $retItem = NULL;

        switch ($contentItemReply->action_type()) {
            case ContentItem_ActionType::ITEM_DETAIL_GET : {
                    $itemGetDetailReply = new ItemDetailGetReply();
                    $itemGetDetailReply->ParseFromString($contentItemReply->message());
                    log_message("info", "[$uuid][ItemDetailGetReply] " . $itemGetDetailReply->toJson());
                    if ($itemGetDetailReply->error_no() != ItemDetailGetReply_ErrorType::SUCCESS) {
                        echo $itemGetDetailReply->error_msg();
                        exit;
                    }

                    $item = new Item();
                    $item = $itemGetDetailReply->item();

                    $profileLite = new ProfileLite();
                    $profileLite = $item->creator();

                    $share = array();
                    $share[1] = $share[2] = $share[3] = 0;
                    for ($i = 0; $i < $item->channel_share_infos_size(); $i++) {
                        $socialChannelShare = new SocialChannelShare();
                        $socialChannelShare = $item->channel_share_info($i);

                        //Số lần share trên fb
                        if ($socialChannelShare->channel_type() == SocialChannelType::FACEBOOK) {
                            $share[1] += 1;
                        }

                        //Số lần share trên Google
                        if ($socialChannelShare->channel_type() == SocialChannelType::GOOGLE) {
                            $share[2] += 1;
                        }

                        //Số lần share trên Zingme
                        if ($socialChannelShare->channel_type() == SocialChannelType::ZINGME) {
                            $share[3] += 1;
                        }
                    }

                    if ($itemGetDetailReply->error_no() == ItemDetailGetReply_ErrorType::SUCCESS) {
                        // parse RageComicAlbumItem from ItemDetailGetReply
                        $item = $itemGetDetailReply->item();
                        switch ($item->type()) {
                            case ItemType::RAGECOMICALBUM : {
                                    $retItem = new RageComicAlbumItem();
                                    $retItem->ParseFromString($item->content());
                                    log_message("info", "[$uuid][RageComicAlbumItem] " . $retItem->toJson());
                                }
                                break;
                            case ItemType::FAQ : {
                                    $retItem = new FAQItem();
                                    $retItem->ParseFromString($item->content());
                                    log_message("info", "[$uuid][FAQItem] " . $retItem->toJson());
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    $arr_retItem = array('retItem' => $retItem, 'share' => $share, 'creator' => $profileLite);
                    return $arr_retItem;
                }
                break;
            case ContentItem_ActionType::UPDATE: {
                    $itemCRUDReply = new ItemCRUDReply();
                    $itemCRUDReply->ParseFromString($contentItemReply->message());
                    log_message("info", "[$uuid][ItemCRUDReply] " . $itemCRUDReply->toJson());
                    $retItem = $itemCRUDReply;
                    return $retItem;
                }
                break;
            case ContentItem_ActionType::CREATE: {
                    $itemCRUDReply = new ItemCRUDReply();
                    $itemCRUDReply->ParseFromString($contentItemReply->message());
                    log_message("info", "[$uuid][ItemCRUDReply] " . $itemCRUDReply->toJson());
                    $retItem = $itemCRUDReply;
                    return $retItem;
                }
                break;
            case ContentItem_ActionType::DELETE: {
                    $itemCRUDReply = new ItemCRUDReply();
                    $itemCRUDReply->ParseFromString($contentItemReply->message());
                    log_message("info", "[$uuid][ItemCRUDReply] " . $itemCRUDReply->toJson());
                    $retItem = $itemCRUDReply;
                    return $retItem;
                }
                break;

            default:
                break;
        }
        log_message("debug", "[$uuid] Parsing reply done.");
    }

    /*     * *************************************************************************
     * function delete content
     * 
     * @param type $uid
     * @param type $uuid
     * @param type $sessionKey
     * @param type $content_id
     * @param type $contentType
     * @return null
     * ************************************************************************* */

    function get_delete_content_request($uid, $uuid, $sessionKey, $content_id, $contentType) {
        $update = time();
        switch ($contentType) {
            case 'comicstrip':
                $comicStripItem = new ComicStripItem();
                $comicStripItem->set_item_id($content_id);
                $comicStripItem->set_title('');
                $comicStripItem->set_update_timestamp($update);
                $comicStripItem->set_adult_only(FALSE);
                $comicStripItem->set_status('');
                $comicStripItem->set_hidden(FALSE);
                $comicStripItemMessage = $comicStripItem->SerializeToString();
                log_message("info", "[$uuid][ComicStripItem]: " . $comicStripItem->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $item = New Item();
                $item->set_type(ItemType::COMICSTRIP);
                $item->set_content($comicStripItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::COMICSTRIP);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
                break;
            case 'album_ragecomics':
                $albumItem = New AlbumItem();
                $albumItem->set_item_id($content_id);
                $albumItem->set_update_timestamp(time());
                $albumItem->set_hidden(true);
                $albumItemMessage = $albumItem->SerializeToString();
                log_message("info", "[$uuid][AlbumItem]: " . $albumItem->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $item = New Item();
                $item->set_type(ItemType::RAGECOMICALBUM);
                $item->set_content($albumItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::RAGECOMICALBUM);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItem]: " . $contentItem->toJson());
                break;
            case 'video':
                $videoItem = New VideoItem();
                $videoItem->set_item_id($content_id);
                $videoItem->set_update_timestamp(time());
                $videoItem->set_hidden(true);
                $videoItem->set_video_uri('');
                $videoItem->set_video_size('');
                $videoItemMessage = $videoItem->SerializeToString();
                log_message("info", "[$uuid][VideoItem]: " . $videoItem->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $item = New Item();
                $item->set_type(ItemType::VIDEO);
                $item->set_content($videoItem);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::VIDEO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
                break;
            case 'movie':
                $movieItem = New MovieItem();
                $movieItem->set_item_id($content_id);
                $movieItem->set_update_timestamp(time());
                $movieItem->set_hidden(true);
                $movieItemMessage = $movieItem->SerializeToString();
                log_message("info", "[$uuid][MovieItem]: " . $movieItem->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);

                $item = New Item();
                $item->set_type(ItemType::MOVIE);
                $item->set_content($movieItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::MOVIE);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemReply]: " . $contentItem->toJson());
                break;
            case 'faq_topic':
                $FAQTopic = New FAQTopic();
                $FAQTopic->set_item_id($content_id);
                $FAQTopic->set_title('');
                $FAQTopic->set_thumb_picture_uri('');
                $FAQTopic->set_update_timestamp('');
                $FAQTopicItemMessage = $FAQTopic->SerializeToString();
                log_message("info", "[$uuid][FAQTopic]: " . $FAQTopic->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);

                $item = New Item();
                $item->set_type(ItemType::FAQ_TOPIC);
                $item->set_content($FAQTopicItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::FAQ_TOPIC);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemReply]: " . $contentItem->toJson());
                break;
            case 'faq_item':
                $FAQItem = New FAQItem();
                $FAQItem->set_item_id($content_id);
                $FAQItem->set_question('');
                $FAQItem->set_answer('');
                $FAQItem->set_update_timestamp('');
                $FAQItemItemMessage = $FAQItem->SerializeToString();
                log_message("info", "[$uuid][FAQItem]: " . $FAQItem->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);

                $item = New Item();
                $item->set_type(ItemType::FAQ);
                $item->set_content($FAQItemItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::FAQ);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemReply]: " . $contentItem->toJson());
                break;
            default:
                $contentMessage = null;
                break;
        }

        return $contentMessage;
    }

    /*     * *************************************************************************
     * function get list of content
     * 
     * @param type $uid
     * @param type $uuid
     * @param type $sessionKey
     * @param type $number
     * @param type $end_timestamp
     * @param type $arr_tag_label
     * @param type $contentType
     * @return type
     * ************************************************************************* */

    function get_list_content_request($uid, $uuid, $sessionKey, $number, $end_timestamp, $arr_tag_label = NULL, $contentType) {
        $itemGetRequest = New ItemGetRequest();
        $itemGetRequest->set_uid($uid);
        $itemGetRequest->set_session_key($sessionKey);
        switch ($contentType) {
            case 'comicstrip':
                $itemGetRequest->append_item_type(ItemType::COMICSTRIP);
                break;
            case 'album_ragecomics':
                $itemGetRequest->append_item_type(ItemType::RAGECOMICALBUM);
                break;
            case 'video':
                $itemGetRequest->append_item_type(ItemType::VIDEO);
                break;
            case 'movie':
                $itemGetRequest->append_item_type(ItemType::MOVIE);
                break;
            case 'faq_topic':
                $itemGetRequest->append_item_type(ItemType::FAQ_TOPIC);
                break;
            case 'faq_item':
                $itemGetRequest->append_item_type(ItemType::FAQ);
                break;
            case 'public_people':
                $itemGetRequest->append_item_type(ItemType::PUBLICPEOPLE);
                break;
            default :
                break;
        }
        if ($arr_tag_label != NULL) {
            foreach ($arr_tag_label as $tag_label) {
                $itemGetRequest->append_tag_label($tag_label);
            }
        }
//        $itemGetRequest->set_sort(ItemGetRequest_SortType::INC);
        $itemGetRequest->set_after_timestamp('');
        $itemGetRequest->set_before_timestamp($end_timestamp);
        $itemGetRequest->set_num_items($number);
        $itemGetRequest->set_include_hidden(TRUE);
        log_message("info", "[$uuid][ItemGetRequest]: " . $itemGetRequest->toJson());
        $itemGetRequestMessage = $itemGetRequest->SerializeToString();

        $contentItem = New ContentItem();
        $contentItem->set_uuid($uuid);
        $contentItem->set_action_type(ContentItem_ActionType::ITEM_GET);
        $contentItem->set_message($itemGetRequestMessage);
        switch ($contentType) {
            case 'comicstrip':
                $contentItem->set_undefined_action_type(ItemType::COMICSTRIP);
                break;
            case 'album_ragecomics':
                $contentItem->set_undefined_action_type(ItemType::RAGECOMICALBUM);
                break;
            case 'video':
                $contentItem->set_undefined_action_type(ItemType::VIDEO);
                break;
            case 'movie':
                $contentItem->set_undefined_action_type(ItemType::MOVIE);
                break;
            case 'faq_topic':
                $contentItem->set_undefined_action_type(ItemType::FAQ_TOPIC);
                break;
            case 'faq_item':
                $contentItem->set_undefined_action_type(ItemType::FAQ);
                break;
            case 'public_people':
                $contentItem->set_undefined_action_type(ItemType::PUBLICPEOPLE);
                break;
            default :
                break;
        }
        log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
        $contentMessage = $contentItem->SerializeToString();

        return $contentMessage;
    }

    /*
     * Get list content    
     */

    function get_list_content_reply($messageReply, $contentType) {
        $contentItemReply = new ContentItem();
        $contentItemReply->ParseFromString($messageReply->last());
        $uuid = $contentItemReply->uuid();
        log_message("info", "[$uuid][ContentItem]: " . $contentItemReply->toJson());

        $itemGetReply = new ItemGetReply();
        $itemGetReply->ParseFromString($contentItemReply->message());
        log_message("info", "[$uuid][ItemGetReply]: " . $itemGetReply->toJson());

        if ($itemGetReply->error_no() == ItemGetReply_ErrorType::SUCCESS) {
            $contentItem = array();
            $creator = array();            
            switch ($contentType) {
                case 'comicstrip':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);
                        $contentItem[$i] = New ComicStripItem();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][ComicStripItem]: " . $contentItem[$i]->toJson());

                        $creator[$i] = New ProfileLite();
                        $creator[$i] = $item->creator();
                    }
                    break;
                case 'album_ragecomics':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);

                        $contentItem[$i] = New RageComicAlbumItem();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][RageComicAlbumItem]: " . $contentItem[$i]->toJson());

                        $creator[$i] = New ProfileLite();
                        $creator[$i] = $item->creator();
                    }
                    break;
                case 'video':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);

                        $contentItem[$i] = New VideoItem();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][VideoItem]: " . $contentItem[$i]->toJson());

                        $creator[$i] = New ProfileLite();
                        $creator[$i] = $item->creator();
                    }
                    break;
                case 'movie':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);

                        $contentItem[$i] = New MovieItem();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][MovieItem]: " . $contentItem[$i]->toJson());

                        $creator[$i] = New ProfileLite();
                        $creator[$i] = $item->creator();
                    }
                    break;
                case 'faq_topic':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);

                        $contentItem[$i] = New FAQTopic();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][FAQTopic]: " . $contentItem[$i]->toJson());
                    }
                    break;
                case 'faq_item':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);

                        $contentItem[$i] = New FAQItem();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][FAQItem]: " . $contentItem[$i]->toJson());

                        $creator[$i] = New ProfileLite();
                        $creator[$i] = $item->creator();
                    }
                    break;
                case 'public_people':
                    for ($i = 0; $i < $itemGetReply->items_size(); $i++) {
                        $item = new Item();
                        $item = $itemGetReply->item($i);

                        $contentItem[$i] = New PublicPeople();
                        $contentItem[$i]->ParseFromString($item->content());
                        log_message("info", "[$uuid][PublicPeople]: " . $contentItem[$i]->toJson());

                        $creator[$i] = New ProfileLite();
                        $creator[$i] = $item->creator();
                    }
                    break;
                default :
                    break;
            }           

            return array('contentItem' => $contentItem, 'creator' => $creator, 'total_items' => $itemGetReply->total_items());
        } else {
            return NULL;
        }
    }

    /*     * *************************************************************************
     * function search content
     * 
     * @param type $uid
     * @param type $uuid
     * @param type $sessionKey
     * @param type $number
     * @param type $begin_index
     * @param type $keywords
     * @return type
     * ************************************************************************* */

    function get_search_content_request($uid, $uuid, $sessionKey, $number, $begin_index, $keywords = NULL, $contentType) {
        $tagSearchRequest = New TagSearchRequest();
        $tagSearchRequest->set_uid($uid);
        $tagSearchRequest->set_session_key($sessionKey);

        $tagSearchRequest->set_type(TagSearchRequest_SearchType::CONTAIN);
        $tagSearchRequest->set_keywords($keywords);
        $tagSearchRequest->set_limit($number);
        $tagSearchRequest->set_begin_index($begin_index);
        $tagSearchRequest->set_include_hidden(FALSE);
        log_message("info", "[$uuid][TagSearchRequest] " . $tagSearchRequest->toJson());
        $tagSearchRequestMessage = $tagSearchRequest->SerializeToString();

        $contentItem = New ContentItem();
        $contentItem->set_uuid($uuid);
        $contentItem->set_action_type(ContentItem_ActionType::TAG_SEARCH);
        $contentItem->set_message($tagSearchRequestMessage);

        switch ($contentType) {
            case 'comicstrip':
                $contentItem->set_undefined_action_type(ItemType::COMICSTRIP);
                break;
            case 'album_ragecomics':
                $contentItem->set_undefined_action_type(ItemType::RAGECOMICALBUM);
                break;
            case 'video':
                $contentItem->set_undefined_action_type(ItemType::VIDEO);
                break;
            case 'movie':
                $contentItem->set_undefined_action_type(ItemType::MOVIE);
                break;
            default :
                break;
        }
        log_message("info", "[$uuid][ContentItem] " . $contentItem->toJson());
        $contentMessage = $contentItem->SerializeToString();
        log_message("debug", "[$uuid] Done creating request.");
        return $contentMessage;
    }

    //Search content Reply
    function get_search_content_reply($messageReply) {
        $contentItemReply = new ContentItem();
        $contentItemReply->ParseFromString($messageReply->last());
        $uuid = $contentItemReply->uuid();
        log_message("info", "[$uuid][ContentItem]: " . $contentItemReply->toJson());

        $tagSearchReply = new TagSearchReply();
        $tagSearchReply->ParseFromString($contentItemReply->message());
        log_message("info", "[$uuid][TagSearchReply]: " . $tagSearchReply->toJson());

        $arr_tag_search = array();

        if ($tagSearchReply->error_no() == TagSearchReply_ErrorType::SUCCESS) {
            if ($tagSearchReply->result() != NULL) {
                $searchResult = new SearchResult();
                $searchResult = $tagSearchReply->result();

                for ($i = 0; $i < $searchResult->tag_labels_size(); $i++) {
                    $arr_tag_search[$i] = $searchResult->tag_label($i);
                }
            }
            return $arr_tag_search;
        } else {
            return NULL;
        }
    }

    /*     * *************************************************************************
     * function full search content
     * 
     * @param type $uid
     * @param type $uuid
     * @param type $sessionKey
     * @param type $number
     * @param type $begin_index
     * @param type $keywords
     * @return type
     * ************************************************************************* */

    function get_full_search_content_request($uid, $uuid, $sessionKey, $number, $begin_index, $keywords = NULL, $contentType) {
        $tagSearchRequest = New TagSearchRequest();
        $tagSearchRequest->set_uid($uid);
        $tagSearchRequest->set_session_key($sessionKey);
        $tagSearchRequest->set_type(TagSearchRequest_SearchType::FULL_SEARCH);
        $tagSearchRequest->set_keywords($keywords);

        switch ($contentType) {
            case 'comicstrip':
                $tagSearchRequest->set_item_type(ItemType::COMICSTRIP);
                break;
            case 'album_ragecomics':
                $tagSearchRequest->set_item_type(ItemType::RAGECOMICALBUM);
                break;
            case 'video':
                $tagSearchRequest->set_item_type(ItemType::VIDEO);
                break;
            case 'movie':
                $tagSearchRequest->set_item_type(ItemType::MOVIE);
                break;
            default :
                break;
        }
        $tagSearchRequest->set_limit($number);
        $tagSearchRequest->set_begin_index($begin_index);
        $tagSearchRequest->set_include_hidden(TRUE);
        log_message("info", "[$uuid][TagSearchRequest]: " . $tagSearchRequest->toJson());
        $tagSearchRequestMessage = $tagSearchRequest->SerializeToString();

        $contentItem = New ContentItem();
        $contentItem->set_uuid($uuid);
        $contentItem->set_action_type(ContentItem_ActionType::TAG_SEARCH);
        $contentItem->set_message($tagSearchRequestMessage);
        $contentItem->set_undefined_action_type(TRUE);
        log_message("debug", "[$uuid][ContentItem]: " . $contentItem->toJson());
        $contentMessage = $contentItem->SerializeToString();

        return $contentMessage;
    }

    //Get full search reply
    function get_full_search_content_reply($messageReply) {
        $contentItemReply = new ContentItem();
        $contentItemReply->ParseFromString($messageReply->last());
        $uuid = $contentItemReply->uuid();
        log_message("debug", "[$uuid][ContentItem]: " . $contentItemReply->toJson());

        $tagSearchReply = new TagSearchReply();
        $tagSearchReply->ParseFromString($contentItemReply->message());
        log_message("info", "[$uuid][TagSearchReply]: " . $tagSearchReply->toJson());

        if ($tagSearchReply->error_no() == TagSearchReply_ErrorType::SUCCESS) {
            $searchResult = new SearchResult();
            $searchResult = $tagSearchReply->result();

            $content = array();
            $creator = array();

            for ($i = 0; $i < $searchResult->items_size(); $i++) {
                $itemResult[$i] = new ItemResult();
                $itemResult[$i] = $searchResult->item($i);

                $content[$i] = new ItemLite();
                $content[$i] = $itemResult[$i]->item();

                $creator[$i] = New ProfileLite();
                $creator[$i] = $content[$i]->creator();
            }
            return array('contentItem' => $content, 'creator' => $creator, 'total_items' => "");
        } else {
            return NULL;
        }
    }

    /*     * *************************************************************************
     * function create photo request
     * 
     * @param type $uid
     * @param type $uuid
     * @param type $sessionKey
     * @param type $album_ragecomics_id
     * @param type $description
     * @param type $photo_uri
     * @param type $album_photo_index
     * @param type $photo_source
     * @param type $photo_size
     * @return type
     * ************************************************************************* */

    function get_create_photos_request($uid, $uuid, $sessionKey, $content_id, $contentType, $description = NULL, $photo_uri = NULL, $album_photo_index = NULL, $photo_source = NULL, $photo_size = NULL) {
        $item_id = '';
        switch ($contentType) {
            case 'album_ragecomics':
                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $rageComicPhoto = New RageComicPhoto();
                $rageComicPhoto->set_item_id($item_id);
                if (isset($description))
                    $rageComicPhoto->set_description($description);
                $rageComicPhoto->set_photo_uri($photo_uri);
                if (isset($photo_source))
                    $rageComicPhoto->set_photo_source($photo_source);
                if (isset($photo_size))
                    $rageComicPhoto->set_photo_size($photo_size);
                $rageComicPhoto->set_album_id($content_id);

                $rageComicPhotoMessage = $rageComicPhoto->SerializeToString();
                log_message("info", "[$uuid][RageComicPhoto]: " . $rageComicPhoto->toJson());

                $item = New Item();
                $item->set_type(ItemType::RAGECOMICPHOTO);
                $item->set_content($rageComicPhotoMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);

                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::CREATE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::RAGECOMICPHOTO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("info", "[$uuid][ContentItem]: " . $contentItem->toJson());
                break;
            case 'comicstrip_chapter':
                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $photoItem = New PhotoItem();
                $photoItem->set_item_id($item_id);
                if (isset($description))
                    $photoItem->set_description($description);
                $photoItem->set_photo_uri($photo_uri);
                $photoItem->set_photo_index($album_photo_index);
                if (isset($photo_source))
                    $photoItem->set_photo_source($photo_source);
                if (isset($photo_size))
                    $photoItem->set_photo_size($photo_size);
                $photoItem->set_container_id($content_id);
                $photoItem->set_update_timestamp(time());
                $photoItem->set_hidden(FALSE);
                $photoItemMessage = $photoItem->SerializeToString();
                $item = New Item();
                $item->set_type(ItemType::PHOTO);
                $item->set_content($photoItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);

                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("info", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::CREATE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::PHOTO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItem]: " . $contentItem->toJson());
                break;
            case 'movie':
                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $moviePhoto = New MoviePhoto();
                $moviePhoto->set_item_id($item_id);
                if (isset($description))
                    $moviePhoto->set_description($description);
                $moviePhoto->set_photo_uri($photo_uri);
                if (isset($photo_source))
                    $moviePhoto->set_photo_source($photo_source);
                if (isset($photo_size))
                    $moviePhoto->set_photo_size($photo_size);
                $moviePhoto->set_movie_id($content_id);

                $moviePhotoMessage = $moviePhoto->SerializeToString();
                log_message("info", "[$uuid][MoviePhoto]: " . $moviePhoto->toJson());
                $item = New Item();
                $item->set_type(ItemType::MOVIEPHOTO);
                $item->set_content($moviePhotoMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::CREATE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::MOVIEPHOTO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
                break;
            default :
                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $photoItem = New PhotoItem();
                $photoItem->set_item_id($item_id);
                if (isset($description))
                    $photoItem->set_description($description);
                $photoItem->set_photo_uri($photo_uri);
                if (isset($album_photo_index)) {
                    $photoItem->set_photo_index($album_photo_index);
                }
                if (isset($photo_source))
                    $photoItem->set_photo_source($photo_source);
                if (isset($photo_size))
                    $photoItem->set_photo_size($photo_size);
                $photoItem->set_container_id($content_id);
                $photoItem->set_update_timestamp(time());
                $photoItem->set_hidden(FALSE);
                $photoItemMessage = $photoItem->SerializeToString();
                $item = New Item();
                $item->set_type(ItemType::PHOTO);
                $item->set_content($photoItemMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);

                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("info", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::CREATE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::PHOTO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItem]: " . $contentItem->toJson());
                break;
        }

        return $contentMessage;
    }

    function update_photo($uid, $session_key, $photo_id, $item_id, $contentType, $description = NULL, $photo_uri = NULL, $photo_source = NULL, $photo_size = NULL, $album_photo_index = NULL) {
        $uuid = uniqid();
        try {
            $photo_update_request = $this->create_update_photo_request($uid, $session_key, $photo_id, $item_id, $contentType, $description, $photo_uri, $photo_source, $photo_size, $album_photo_index);
            if ($photo_update_request != NULL) {
                $zmsg_request = new zmsg();
                $zmsg_request->body_set($photo_update_request);
                $zmsg_reply = $this->mdpClient->send("contents", $zmsg_request);
                $itemCRUDReply = $this->parse_reply($zmsg_reply->last());

                if ($itemCRUDReply->error_no() == ItemCRUDReply_ErrorNo::SUCCESS) {
                    log_message("info", "[$uuid]Successfully update photo (id: $photo_id)");
                    return TRUE;
                } else {
                    log_message("error", "[$uuid]Can not update photo (id: $photo_id)");
                    return FALSE;
                }
            } else {
                log_message("error", "[$uuid]Can not create photo update request");
                return FALSE;
            }
        } catch (MY_AcxException $mae) {
            log_message("error", "[$uuid]" . $mae->to_json());
            return FALSE;
        }
    }

    function create_update_photo_request($uid, $session_key, $photo_id, $content_id, $contentType, $description = NULL, $photo_uri = NULL, $photo_source = NULL, $photo_size = NULL, $album_photo_index = NULL) {
        $uuid = uniqid();
        switch ($contentType) {
            case 'album_ragecomics':
                $ragecomic_photo = new RageComicPhoto();
                $ragecomic_photo->set_item_id($photo_id);
                $ragecomic_photo->set_photo_uri($photo_uri);
                $ragecomic_photo->set_description($description);
                if ($photo_source != NULL)
                    $ragecomic_photo->set_photo_source($photo_source);
                if ($photo_size != NULL)
                    $ragecomic_photo->set_photo_size($photo_size);
                if ($content_id != NULL)
                    $ragecomic_photo->set_album_id($content_id);
                if ($album_photo_index != NULL)
                    $ragecomic_photo->set_album_photo_index($album_photo_index);
                log_message("info", "[$uuid][RagecomicPhoto]: " . $ragecomic_photo->toJson());


                $item = New Item();
                $item->set_type(ItemType::RAGECOMICPHOTO);
                $item->set_content($ragecomic_photo->SerializeToString());
                break;

            default:
                $photoItem = new PhotoItem();
                $photoItem->set_item_id($photo_id);
                $photoItem->set_photo_uri($photo_uri);
                if ($description != NULL)
                    $photoItem->set_description($description);
                if ($photo_source != NULL)
                    $photoItem->set_photo_source($photo_source);
                if ($photo_size != NULL)
                    $photoItem->set_photo_size($photo_size);
                if ($content_id != NULL)
                    $photoItem->set_album_id($content_id);
                $photoItem->set_photo_index($album_photo_index);
                $photoItem->set_hidden(TRUE);
                $photoItem->set_update_timestamp(time());
                log_message("info", "[$uuid][PhotoItem]: " . $photoItem->toJson());

                $item = New Item();
                $item->set_type(ItemType::PHOTO);
                $item->set_content($photoItem->SerializeToString());
                break;
        }

        $ItemCRUDRequest = New ItemCRUDRequest();
        $ItemCRUDRequest->set_uid($uid);
        $ItemCRUDRequest->set_session_key($session_key);
        $ItemCRUDRequest->set_item(0, $item);
        $ItemCRUDRequest->set_create_feed(TRUE);
        log_message("info", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
        $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

        $contentItem = New ContentItem();
        $contentItem->set_uuid($uuid);
        $contentItem->set_action_type(ContentItem_ActionType::UPDATE);
        $contentItem->set_message($ItemCRUDRequestMessage);
        $contentItem->set_undefined_action_type(false);
        $contentMessage = $contentItem->SerializeToString();
        log_message("info", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());

        return $contentMessage;
    }

    /**
     * Function Delete Photo
     * 
     * @param type $uid
     * @param type $session_key
     * @param type $photo_id
     * @param type $item_id
     * @param type $contentType
     * @return boolean
     */
    function delete_photo($uid, $session_key, $photo_id, $item_id, $contentType) {
        $uuid = uniqid();
        try {
            $photo_delete_request = $this->create_delete_photo_request($uid, $session_key, $photo_id, $item_id, $contentType);

            if ($photo_delete_request != NULL) {
                $zmsg_request = new zmsg();
                $zmsg_request->body_set($photo_delete_request);
                $zmsg_reply = $this->mdpClient->send("contents", $zmsg_request);
                
                $itemCRUDReply = $this->parse_reply($zmsg_reply->last());
                if ($itemCRUDReply->error_no() == ItemCRUDReply_ErrorNo::SUCCESS) {
                    log_message("info", "[$uuid]Successfully delete photo (id: $item_id)");
                    return TRUE;
                }
            } else {
                log_message("error", "[$uuid]Can not create album detail request");
                return FALSE;
            }
        } catch (MY_AcxException $mae) {
            log_message("error", "[$uuid]" . $mae->to_json());
            return FALSE;
        }
    }

    function create_delete_photo_request($uid, $sessionKey, $photo_id, $item_id, $contentType) {

        $uuid = uniqid();
        switch ($contentType) {
            case 'album_ragecomics':
                $rageComicPhoto = New RageComicPhoto();
                $rageComicPhoto->set_item_id($photo_id);
                $rageComicPhoto->set_photo_uri('');
                $rageComicPhoto->set_album_id($item_id);
                log_message("info", "[$uuid][RageComicPhoto]: " . $rageComicPhoto->toJson());
                $rageComicPhotoMessage = $rageComicPhoto->SerializeToString();

                $item = New Item();
                $item->set_type(ItemType::RAGECOMICPHOTO);
                $item->set_content($rageComicPhotoMessage);

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();

                $contentItem = New ContentItem();

                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::RAGECOMICPHOTO);
                log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
                $contentMessage = $contentItem->SerializeToString();
                break;
            case 'comicstrip_chapter':
                $photoItem = New PhotoItem();
                $photoItem->set_item_id($photo_id);
                $photoItem->set_photo_uri('');
                $photoItem->set_container_id($item_id);
                $photoItem->set_hidden(FALSE);
                $photoItem->set_update_timestamp('');
                $photoItem->set_photo_index('');
                $photoItemMessage = $photoItem->SerializeToString();
                log_message("info", "[$uuid][PhotoItem]: " . $photoItem->toJson());

                $item = New Item();
                $item->set_type(ItemType::PHOTO);
                $item->set_content($photoItemMessage);

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::PHOTO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
                break;
            case 'movie':
                $moviePhoto = New MoviePhoto();
                $moviePhoto->set_item_id($photo_id);
                $moviePhoto->set_photo_uri('');
                $moviePhoto->set_movie_id($item_id);
                $moviePhotoMessage = $moviePhoto->SerializeToString();
                log_message("info", "[$uuid][MoviePhoto]: " . $moviePhoto->toJson());

                $ItemCRUDRequest = New ItemCRUDRequest();
                $ItemCRUDRequest->set_uid($uid);
                $ItemCRUDRequest->set_session_key($sessionKey);

                $item = New Item();
                $item->set_type(ItemType::MOVIEPHOTO);
                $item->set_content($moviePhotoMessage);
                $ItemCRUDRequest->set_item(0, $item);
                $ItemCRUDRequest->set_create_feed(TRUE);
                $ItemCRUDRequestMessage = $ItemCRUDRequest->SerializeToString();
                log_message("debug", "[$uuid][ItemCRUDRequest]: " . $ItemCRUDRequest->toJson());

                $contentItem = New ContentItem();
                $contentItem->set_uuid($uuid);
                $contentItem->set_action_type(ContentItem_ActionType::DELETE);
                $contentItem->set_message($ItemCRUDRequestMessage);
                $contentItem->set_undefined_action_type(ItemType::MOVIEPHOTO);
                $contentMessage = $contentItem->SerializeToString();
                log_message("debug", "[$uuid][ContentItemRequest]: " . $contentItem->toJson());
                break;
            default :
                $contentMessage = NULL;
                break;
        }
        return $contentMessage;
    }

    /**
     * Function Get Detail Content
     * 
     * @param type $uid
     * @param type $session_key
     * @param type $item_id
     * @param type $contentType
     * @return null
     */
    function get_content_detail($uid, $session_key, $item_id, $contentType) {
        $content_detail_request = $this->create_content_detail_request($uid, $session_key, $item_id, $contentType);
        if ($content_detail_request != NULL) {
            $zmsg_request = new zmsg();
            $zmsg_request->body_set($content_detail_request);
            $zmsg_reply = $this->mdpClient->send("contents", $zmsg_request);

            return $this->parse_reply($zmsg_reply->last());
        } else {
            log_message("error", "Can not get detail request");
            return NULL;
        }
    }

    function create_content_detail_request($uid, $sessionKey, $item_id, $contentType) {
        $uuid = uniqid();
        $itemDetailGet = New ItemDetailGetRequest();
        $itemDetailGet->set_item_id($item_id);
        $itemDetailGet->set_uid($uid);
        $itemDetailGet->set_session_key($sessionKey);
        $itemDetailGet->set_include_hidden(TRUE);
        $itemDetailGetMessage = $itemDetailGet->SerializeToString();
        log_message("info", "[$uuid][ItemDetailGetRequest]: " . $itemDetailGet->toJson());

        $contentItem = New ContentItem();
        $contentItem->set_uuid($uuid);
        $contentItem->set_action_type(ContentItem_ActionType::ITEM_DETAIL_GET);
        $contentItem->set_message($itemDetailGetMessage);
        switch ($contentType) {
            case 'faq_item':
                $contentItem->set_undefined_action_type(ItemType::FAQ);
                break;
        }
        $contentMessage = $contentItem->SerializeToString();
        log_message("info", "[$uuid][ContentItem]: " . $contentItem->toJson());

        return $contentMessage;
    }

}